package zheezes.protocol.impl.gd;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import zheezes.protocol.impl.gd.bean.VipMeterData;
import zheezes.protocol.impl.gd.converter.DateConverter;


public class Ctrl93Frame extends GdFrame {
	private byte resend;
	private byte total;
	private byte cursor;
	private Calendar cal = Calendar.getInstance();
	private byte days;
	private List<VipMeterData> list = new ArrayList<VipMeterData>();

	@Override
	public int sizeOfData() {
		int size = 7;
		if(list.size() > 0) {
			size += list.get(0).size();
		}
		return size;
	}
	
	@Override
	public int getData(byte[] buf, int offset) {
		int idx = offset;
		buf[idx++] = resend;
		buf[idx++] = total;
		buf[idx++] = cursor;
		DateConverter.vtob(buf, idx, cal);
		idx += 3;
		buf[idx++] = days;
		for (VipMeterData data : list) {
			idx += data.getBytes(buf, idx);
		}
		idx += 2; // meter m seq
		return idx - offset;
	}

	@Override
	public int setData(byte[] buf, int offset, int max) {
		int idx = offset;
		resend = buf[idx++];
		total = buf[idx++];
		cursor = buf[idx++];
		cal = DateConverter.btov(buf, idx);
		idx += 3;
		days = buf[idx++];
		while (idx < max - 2) {
			VipMeterData data = new VipMeterData();
			idx += data.setBytes(buf, idx, max);
			list.add(data);
		}
		idx += 2; // meter m seq
		return idx - offset;
	}
	
	public VipMeterData[] getVipMeterData() {
		return list.toArray(new VipMeterData[0]);
	}
	
	public Date getDate() {
		return cal.getTime();
	}
}
